import actionTypes from '../actions/actionTypes';
//authToken表示用户登录状态
//数据在userStorageInfo里面
const isLogin =
  window.localStorage.getItem('authToken') ||
  window.sessionStorage.getItem('authToken');
const userInfo =
  window.localStorage.getItem('userStorageInfo') ||
  window.sessionStorage.getItem('userStorageInfo');
const initState = {
  ...JSON.parse(userInfo),
  isLogin,
  isLoading: false
};
//处理登录问题：1
/**
 * 1、index页面如果没有登录就不能进入App
 */
export default (state = initState, action) => {
  switch (action.type) {
    case actionTypes.START_LOGIN:
      return {
        ...state,
        isLoading: true,
        isLogin: false
      };
    case actionTypes.LOGIN_SUCCESS:
      return {
        ...state,
        ...action.payLoad,
        isLoading: false,
        isLogin: true
      };
    case actionTypes.LOGIN_FAILED:
      return {
        id: '',
        displayName: '',
        avatar: '',
        role: '',
        isLoading: false,
        isLogin: false
      }; //如果登录失败，保存登录签的状态
    case actionTypes.CHANGE_AVATAR:
      return {
        ...state,
        avatar: action.payLoad.avatar
      };
    default:
      return state;
  }
};
